﻿using Authentication.Core.Configuration;
using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Data.Repository.Enum;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class UpdateResultCreateDaoByListServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateCreateDaoRequestRequest, ResponseRecord>
    {
        public UpdateResultCreateDaoByListServiceCommand(IProviderFactory providers) : base(providers) { }
        #region Private Properties
        private List<RequestCreateDaoRecord> ListRequest = new List<RequestCreateDaoRecord>();
        private UserRecord currentUser;
        #endregion
        protected override void Validate(UpdateCreateDaoRequestRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            if (request.ListCreateDaoRequest == null || request.ListCreateDaoRequest.Count == 0)
            {
                throw new ArgumentException("List record must be set", "request");
            }
            else
            {
                ListRequest = request.ListCreateDaoRequest;
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            bool savedUser = false;
            if (SystemConfiguration.UserBICApprovedDao.Contains(currentUser.UserName))
            {
                try
                {
                    var userQuestion = ListRequest.Select(record => new CreateDaoRequest
                    {
                        Status = (int)StatusRequestCreateDaoEnum.Done,
                        StaffId = record.StaffId,
                        Cif = record.Cif,
                        Dao = record.Dao
                    }).ToList();

                    savedUser = Providers.RequestDao.UpdateListResultCreateDaoRequest(userQuestion);
                    //response.IsSuccess = savedUser;
                }
                catch (Exception ex)
                {
                    response.ErrorMessage = ex.Message;
                }
            }
            else
            {
                response.ErrorMessage = string.Format("User {0} not have permission", currentUser.UserName);
            }
            return savedUser;
        }
    }
}
